﻿using System;
using System.Diagnostics;
using System.IO;
using Microsoft.Win32;

namespace CSharp.Common.File
{
    public class WinrarHelper
    {
        
        public void CompressRAR(string patch, string rarPatch, string rarName)
        {
            try
            {
                RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
                string str = key.GetValue("").ToString();
                key.Close();
                str = str.Substring(1, str.Length - 7);
                Directory.CreateDirectory(patch);
                string str2 = " a    " + rarName + "  " + patch + "  -r";
                ProcessStartInfo info = new ProcessStartInfo
                {
                    FileName = str,
                    Arguments = str2,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    WorkingDirectory = rarPatch
                };
                Process process = new Process
                {
                    StartInfo = info
                };
                process.Start();
                process.WaitForExit();
                process.Close();
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        public static bool IsExists()
        {
            return !string.IsNullOrEmpty(Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe").GetValue("").ToString());
        }

        public string UNCompressRAR(string unRarPatch, string rarPatch, string rarName, bool deleteFlag)
        {
            try
            {
                RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
                string str = key.GetValue("").ToString();
                key.Close();
                if (!Directory.Exists(unRarPatch))
                {
                    Directory.CreateDirectory(unRarPatch);
                }
                string str2 = "x " + rarName + " " + unRarPatch + " -y";
                ProcessStartInfo info = new ProcessStartInfo
                {
                    FileName = str,
                    Arguments = str2,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    WorkingDirectory = rarPatch
                };
                Process process = new Process
                {
                    StartInfo = info
                };
                process.Start();
                process.WaitForExit();
                process.Close();
                if (deleteFlag && System.IO. File.Exists(rarPatch + @"\" + rarName))
                {
                    System.IO.File.Delete(rarPatch + @"\" + rarName);
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return unRarPatch;
        }
    }
}

